// src/api/auth.ts

// 模拟数据库（内存存储）
const users: { username: string; password: string }[] = [{username:'HS',password:'123'}]

export function loginApi(data: { username: string; password: string }) {
  return new Promise((resolve, reject) => {
    const user = users.find(
      (u) => u.username === data.username && u.password === data.password
    )
    if (user) {
      resolve({
        token: 'fake-jwt-token-' + Date.now(),
        username: user.username,
      })
    } else {
      reject(new Error('用户名或密码错误'))
    }
  })
}

export function registerApi(data: { username: string; email: string; password: string }) {
  return new Promise((resolve, reject) => {
    const exists = users.some((u) => u.username === data.username)
    if (exists) {
      reject(new Error('该用户名已存在'))
    } else {
      users.push({ username: data.username, password: data.password })
      resolve({ message: '注册成功' })
    }
  })
}
